import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
class Solution {
    boolean[][] visited;
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        visited = new boolean[m][n];
        int ret = 0;//返回最大的岛屿面积
        Queue<int[]> queue = new LinkedList();
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(grid[i][j] == 1 && visited[i][j] == false){
                    queue.offer(new int[]{i, j});
                    int CurArea = 1;
                    visited[i][j] = true;
                    while(!queue.isEmpty()){
                        int[] poll = queue.poll();
                        for(int k = 0; k < 4; k++){
                            int x = poll[0] + dx[k];
                            int y = poll[1] + dy[k];
                            if(x >= 0 && x < m && y >= 0 && y < n
                                    && grid[x][y] == 1 && visited[x][y] == false){
                                CurArea++;
                                visited[x][y] = true;
                                queue.offer(new int[]{x, y});
                            }
                        }
                    }
                    ret = Math.max(ret, CurArea);
                }
            }
        }

        return ret;
    }
}